<?php

function getProjectRootPath(){
    $patharr=explode("/",$_SERVER["REQUEST_URI"]);
    
    if(!strstr($_SERVER["DOCUMENT_ROOT"],$patharr[1])){
        if(strtolower($_SERVER["SERVER_NAME"])=="localhost"){
            $afterWampToProjectRootPath1=$_SERVER["DOCUMENT_ROOT"]."/".$patharr[1];
            if(isset($patharr) && is_array($patharr) && count($patharr)>2 && !strstr($patharr[2],".")){
                $afterWampToProjectRootPath1=$afterWampToProjectRootPath1."/".$patharr[2];
            }   
        }
        else if(strstr($_SERVER["SERVER_NAME"],"allyfolio")){
            $afterWampToProjectRootPath1="/home/adminvikrant/public_html";
        }
        else{
            $afterWampToProjectRootPath1=$_SERVER["DOCUMENT_ROOT"];
            if(isset($patharr) && is_array($patharr) && count($patharr)>2 && !strstr($patharr[1],".")){
                $afterWampToProjectRootPath1=$afterWampToProjectRootPath1."/".$patharr[1];
            }
        }
        $afterWampToProjectRootPath1.="/";
        $_SERVER["DOCUMENT_ROOT"]=$afterWampToProjectRootPath1;
    }
    /*
    $afterWampToProjectRootPath="/allyfolio-live/allyfolio";
    $localpath=getenv("SCRIPT_NAME");
    $absolutepath=getenv("SCRIPT_FILENAME");
    $docRoot=substr($absolutepath,0,strpos($absolutepath,$localpath));
    $_SERVER['DOCUMENT_ROOT']= $docRoot.$afterWampToProjectRootPath;
    */
}

function cleanInput($input) {

  $search = array(
    '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
  );
 
    $output = preg_replace($search, '', $input);
    return $output;
}
  
function sanitize($input) {
    if (is_array($input)) {
        foreach($input as $var=>$val) {
            $output[$var] = sanitize($val);
        }
    }
    else {
        $input  = cleanInput($input);
        $output=$input;
    }
    return $output;
}

function redirect($path)
{
	?>
	<script>
		window.location="<?php echo $path; ?>";
	</script>
	<?php
}

function convertDateToTime($date)
{
	if(trim($date)!="")
	{
		$datearr=explode("/",$date);
		$nowtimedate=mktime(0,0,1,$datearr[1],$datearr[0],$datearr[2]);
		return $nowtimedate;
	}
	else
	{
		return "";
	}
}

function convertdate($datestr="")
{
	$datearr=explode("-",$datestr);
	if($datearr[2]!="0000")
	{
		return ($datearr[2]."/".$datearr[1]."/".$datearr[0]);
	}
	else
	{
		return "NA";
	}
}

function getuniquearraybasedvalues($nonuniquearr=array())
{
    $uniquearr=array();
    
    for($i=0;$i<count($nonuniquearr);$i++)
    {
        $flag=0;
        for($j=($i+1);$j<count($nonuniquearr);$j++)
        {
            if(trim($nonuniquearr[$i])==trim($nonuniquearr[$j]))
            {
                $flag=1;
                break;
            }
        }
        if($flag==0)
        {
            $uniquearr[]=$nonuniquearr[$i];
        }
    }
    return $uniquearr;
}

function getTimestamp($time)
{
	$arr_time=explode(".",$time);
	$total_sec=$arr_time[0]*60*60;
	if(isset($arr_time[1]))
	{
		$total_sec+=($arr_time[1]*60);
	}
	return $total_sec;
}
function convertTimeInNormal($time)
{
	$outstrtime=$time;
	$outstrtime=floatval(str_ireplace(":",".",$outstrtime));
	if($outstrtime>24)
	{
		$outstrtime=$outstrtime-24;
	}
	$outstrtime=str_ireplace(".",":",strval($outstrtime));
	if(!strstr($outstrtime,":"))
	{
		$outstrtime=$outstrtime.":00";
	}
	if(strstr($outstrtime,":3"))
	{
		$outstrtime=$outstrtime."0";
	}
	return $outstrtime;
}

function make_dropdown($arr_shifts,$id,$placeholdermsg,$name="",$selectedvalues="",$onchangefunction="javascript:void(0);")
{
	if($name=="")
	{
		$name=$id; 
	}
	$returnstr="";
	$returnstr.='<select id="'.$id.'" name="'.$name.'" onchange="'.$onchangefunction.'">
		<option value="">'.$placeholder.'</option>
		';
		if(count($arr_shifts)>0)
		{
			for($i=0;$i<count($arr_shifts);$i++)
			{
				if($selectedvalues!="")
				{
					if(stristr($arr_shifts[$i],$selectedvalues))
						$returnstr.='<option value="'.$arr_shifts[$i].'" selected>'.$arr_shifts[$i].'</option>';
					else
						$returnstr.='<option value="'.$arr_shifts[$i].'" >'.$arr_shifts[$i].'</option>';
				}
				else
				{
					$returnstr.='<option value="'.$arr_shifts[$i].'" >'.$arr_shifts[$i].'</option>';
				}
			}
		}
		
	$returnstr.='</select>';
	return $returnstr;
	?>
	
	<?php
}

function createinsertquerybasedonparams($tables="users",$fieldarray=array())
{
    $sqlquery="insert into ".$tables." set ";
    if(count($fieldarray)>0)
    {
        foreach($fieldarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key.",";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-1);
    }
    return $sqlquery;
}

function getbindarray($fieldarray=array())
{
    $bindarray=array();
    foreach($fieldarray as $key=>$value)
	{
		$bindarray[":".$key]=$value;
	}
    return $bindarray;
}

function createupdatequerybasedonparams($tables="users",$fieldarray=array(),$conditionarray=array())
{
    $sqlquery="update ".$tables." set ";
    if(count($fieldarray)>0)
    {
        foreach($fieldarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key.",";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-1);
    }
    if(count($conditionarray)>0)
    {
        $sqlquery.=" where ";
        foreach($conditionarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key." and ";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-4);
    }
    
    return $sqlquery;
}

function createdeletebasedonparams($tables="users",$conditionarray=array())
{
    $sqlquery="delete from ".$tables." ";
    if(count($conditionarray)>0)
    {
        $sqlquery.=" where ";
        foreach($conditionarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key." and ";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-4);
    }
    
    return $sqlquery;
}

function random_string($length) {
    $key = '';
    $keys = array_merge(range(0, 9), range('a', 'z'));

    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }

    return $key;
}

function toPublicId($id) {
  return $id * 14981488888 + 8259204988888;
}

function toInternalId($publicId) {
  return ($publicId - 8259204988888) / 14981488888;
}


function getCommonFriends($pdoobj,$firstuserid=0,$seconduserid=0)
{
    $firstuserfriends=$pdoobj->fetch_array($pdoobj->query("select Active_Friends_IDs from af_users where User_ID=:userfriendid",array("userfriendid"=>$firstuserid)));
    $seconduserfriends=$pdoobj->fetch_array($pdoobj->query("select Active_Friends_IDs from af_users where User_ID=:userfriendid",array("userfriendid"=>$seconduserid)));
    if(count($firstuserfriends)==0 || count($seconduserfriends)==0)
    {
        return 0;
    }
    else
    {
        $firstfriendsarr=explode(",",$firstuserfriends[0]['Active_Friends_IDs']);
        $secofriendsarr=explode(",",$seconduserfriends[0]['Active_Friends_IDs']);
        $count=0;
        
        for($i=0;$i<count($firstfriendsarr);$i++)
        {
            if(in_array($firstfriendsarr[$i],$secofriendsarr) && trim($firstfriendsarr[$i])!="")
            {
                $count++;
            }
        }
        return $count;
    }
}
function create_slug($string)
{
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
function datediff($start,$end ) 
{
	$diff=$end-$start;

	$dateDiffArr['years']=@floor($diff/(365*24*60*60));
	$remainingdiff=$diff%(365*24*60*60);
	
	$dateDiffArr['months']=@floor($remainingdiff/(30*24*60*60));
	$remainingdiff=$remainingdiff%(30*24*60*60);
	
	$dateDiffArr['days']=@floor($remainingdiff/(24*60*60));
	$remainingdiff=$remainingdiff%(24*60*60);
	
	$dateDiffArr['hours']=@floor($remainingdiff/(60*60));
	$remainingdiff=$remainingdiff%(60*60);
	
	$dateDiffArr['mins']=@floor($remainingdiff/(60));
	$dateDiffArr['secs']=$remainingdiff%(60);
		
	return $dateDiffArr;
}
function getTimeAgo($start, $end)
{
	$dateDiffArr=datediff($start,$end);

	if($dateDiffArr["days"]>7)
	{
		$sentDate=date("M d",$start);
	}
	else if($dateDiffArr["days"]>=1)
	{
		$sentDate=$dateDiffArr["days"]."d";
	}
	else if($dateDiffArr["hours"]>=1)
	{
		$sentDate=$dateDiffArr["hours"]."h";
	}
	else if($dateDiffArr["mins"]>=1)
	{
		$sentDate=$dateDiffArr["mins"]."min";
	}
	else
	{
		$sentDate="Now";
	}
	return $sentDate;
}
function convertVideo($fullPathAfterMoveUploadedFile, $fileNameWithoutExtension)
{
	$fullPathAfterConversionIncludingExtension=$fullPathAfterMoveUploadedFile;
	return $fullPathAfterConversionIncludingExtension;
}

?>